#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/7/25 下午8:42
# @Author  : fugang_le
# @Software: PyCharm

import math
import jieba
import re

dianya = ['0.4', '6.6', '10', '35', '66', '110', '220', '330', '500', '750', '1100', '1500']


def euclidean_distance():
    '''
    欧式距离
    :return:
    '''
    import numpy as np

    vector1 = np.array([1, 2, 3])
    vector2 = np.array([4, 5, 6])

    op1 = np.sqrt(np.sum(np.square(vector1 - vector2))) # 方法1
    # 求2阶范数
    op2 = np.linalg.norm(vector1 - vector2) #方法2  np.linalg == linear(线性) + algebra(代数)


def manhattan_distance():
    '''
    曼哈顿距离
    :return:
    '''
    import numpy as np
    vector1 = np.array([1, 2, 3])
    vector2 = np.array([4, 5, 6])

    op3 = np.sum(np.abs(vector1 - vector2))
    op4 = np.linalg.norm(vector1 - vector2, ord=1) # ord参数表示一阶范数


def cosine():
    '''
    余弦夹角
    :return:
    '''
    import numpy as np

    vector1 = np.array([1, 2, 4])
    vector2 = np.array([1, 2, 3])

    op7 = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * (np.linalg.norm(vector2)))
    return op7


def hamming_distance():
    '''
    汉明距离
    :return:
    '''
    import numpy as np

    v1 = np.array([1, 1, 0, 1, 0, 1, 0, 0, 1])
    v2 = np.array([0, 1, 1, 0, 0, 0, 1, 1, 1])
    smstr = np.nonzero(v1 - v2)
    print(smstr)    # 不为0 的元素的下标
    sm = np.shape(smstr[0])[0]
    print(sm)


def jaccard_distance():
    '''
    杰卡德距离
    :return:
    '''

    import scipy.spatial.distance as dist
    import numpy as np

    v1 = np.array([1, 1, 0, 1, 0, 1, 0, 0, 1])
    v2 = np.array([0, 1, 1, 0, 0, 0, 1, 1, 1])

    matv = np.array([v1, v2])
    print(matv)
    ds = dist.pdist(matv, 'jaccard')
    print(ds)


def word_handle(words):
    # 电压处理
    new_words = []
    for word in words:
        # 去除ac
        ac_match = re.search(r'(?<=ac)\d+(?=kv)', word)
        kv_match = re.search(r'\d+(?=(kv|mm|mva|ka))', word)
        if ac_match:
            new_words.append(ac_match.group())
        elif kv_match:
            new_words.append(kv_match.group())
        elif word in dianya:
            new_words.append(word)

    return new_words


def number_handle(words):
    # 电压处理
    new_words = []
    for word in words:
        # 去除ac
        number_match = re.findall(r'(\d+)',word)
        new_words.extend(number_match)
    return new_words


def number_Jaccrad(text1, text2):  # terms_reference为源句子，terms_model为候选句子
    text1 = text1.lower().strip()
    text2 = text2.lower().strip()
    words1= jieba.lcut(text1)  # 默认精准模式
    words2 = jieba.lcut(text2)
    words1 =[word for word in words1 if re.search(r'\d', word)]
    words2 = [word for word in words2 if re.search(r'\d', word)]

    #  处理没待单位的数字属性，例如：220kv == 220，600mm == 600, ac110kv == 110kv
    words1 = word_handle(words1)
    words2 = word_handle(words2)

    temp = len(set(words1) & set(words2))
    fenmu = len(set(words1) | set(words2))  # 并集
    fenmu = len(set(words1))
    if not fenmu:
        return 0
    jaccard_coefficient = float(temp / fenmu)  # 交集
    return jaccard_coefficient


def letter_jaccrad(text1, text2):  # terms_reference为源句子，terms_model为候选句子
    text1 = text1.lower().strip()
    text2 = text2.lower().strip()
    words1= jieba.lcut(text1)  # 默认精准模式
    words2 = jieba.lcut(text2)
    words1 =[word for word in words1 if not re.search(r'\d', word)]
    words2 = [word for word in words2 if not re.search(r'\d', word)]
    #  处理没待单位的数字属性，例如：220kv == 220，600mm == 600, ac110kv == 110kv
    # words1 = word_handle(words1)
    # words2 = word_handle(words2)
    # print(words1)
    # print(words2)
    temp = len(set(words1) & set(words2))
    fenmu = len(set(words1) | set(words2))  # 并集
    if not fenmu:
        return 0
    jaccard_coefficient = float(temp / fenmu)  # 交集
    return jaccard_coefficient


def sigmoid(x):
    return 1 / (1 + math.e ** -(x/10))


def LCS(str1, str2): 
    lstr1 = len(str1)
    lstr2 = len(str2)
    record = [[0 for i in range(lstr2+1)] for j in range(lstr1+1)] # 多一位
    maxNum = 0   # 最长匹配长度
    p = 0    # 匹配的起始位
    
    for i in range(lstr1):
        for j in range(lstr2):
            if str1[i] == str2[j]:
                # 相同则累加
                record[i+1][j+1] = record[i][j] + 1
                if record[i+1][j+1] > maxNum:
                    # 获取最大匹配长度
                    maxNum = record[i+1][j+1]
                    # 记录最大匹配长度的终止位置
                    p = i + 1
    return maxNum / lstr1


if __name__ == '__main__':
    import jieba.posseg as pos
    text1 = '500kV母线保护柜（每面含500kV母线保护装置2台）'
    text2 = '500kv母线保护屏'
    # text2 = '主变压器500kv 240mva 单相无载调压'
    print(jieba.lcut(text1))
    print(jieba.lcut(text2))
    s = number_Jaccrad(text1, text2)
    print(s)
    # number_match = re.findall(r'(\d+)', text2)
    # print(number_match)

